﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace System.ComponentModel {
    /// <summary>
    /// 枚举类的扩展
    /// </summary>
    public static class ExtendDescriptionAttribute {
        /// <summary>
        /// 枚举类类型对象特性值Description
        /// </summary>
        /// <param name="em"></param>
        /// <returns></returns>
        public static string GetDescription(this Enum em) {
            string? retString = string.Empty;

            Type type = em.GetType();
            System.Reflection.FieldInfo? fd = type.GetField(em.ToString());
            if (fd != null) {
                retString = (fd.GetCustomAttributes(typeof(DescriptionAttribute), false).FirstOrDefault() as DescriptionAttribute)?.Description;
            }

            return retString;
        }
        public static string GetDescription(this object objectXX) {
            string retString = string.Empty;

            Type type = objectXX.GetType();
            if (type.GetCustomAttribute(typeof(DescriptionAttribute), false) is DescriptionAttribute tempDescription) {
                return tempDescription.Description;
            }

            return retString;
        }
    }
}
